package com.hgl.bi.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgl.bi.common.constant.ErrorCode;
import com.hgl.bi.common.utils.AssertUtil;
import com.hgl.bi.web.entity.RoleEntity;
import com.hgl.bi.web.mapper.RoleMapper;
import com.hgl.bi.web.model.dto.RolePmtDto;
import com.hgl.bi.web.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: BI
 * @description: 角色服务实现层
 * @author: hgl
 * @create: 2025-02-12 16:14
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity>
        implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void awardPermission(RolePmtDto rolePmtDto) {
        Long roleId = rolePmtDto.getRoleId();
        List<Long> pmtIds = rolePmtDto.getPmtIds();
        // 非空校验
        AssertUtil.notNull(roleId, ErrorCode.ROLE_ID_NULL);
        AssertUtil.notNull(pmtIds, ErrorCode.PMT_ID_NULL);
        // 分配权限
        boolean awarded = roleMapper.awardPermission(roleId, pmtIds);
        AssertUtil.isTrue(awarded, ErrorCode.AWARD_PERMISSION_FALSE);
    }

    @Override
    public List<Long> getRolePermissions(Long id) {
        List<Long> permissions = roleMapper.selectRolePermissions(id);
        return permissions;
    }

    @Override
    public void removeRolePermission(RolePmtDto rolePmtDto) {
        Long roleId = rolePmtDto.getRoleId();
        List<Long> pmtIds = rolePmtDto.getPmtIds();
        // 非空校验
        AssertUtil.notNull(roleId, ErrorCode.ROLE_ID_NULL);
        AssertUtil.notNull(pmtIds, ErrorCode.PMT_ID_NULL);
        // 删除权限
        boolean removed = roleMapper.removePermission(roleId, pmtIds);
        AssertUtil.isTrue(removed, ErrorCode.REMOVE_FALSE);
    }


}
